package {
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.system.Security;
	import flash.text.TextField;
	import flash.ui.ContextMenu;
	
	import gs.TweenLite;
	import gs.easing.*;
	
	import org.ilovecode.animation.*;
	import org.ilovecode.display.*;
	import org.ilovecode.events.*;
	import org.ilovecode.mvc.View;
	import org.ilovecode.screen.Screen;
	import org.ilovecode.screen.ScreenName;
	import org.ilovecode.utils.Utilities;
	
	/**
	 * ...
	 * @author Vjekoslav Ratkajec
	 */
	public class Main extends View {
		
		//{ region Stage elements
		
		//} endregion
		
		private var intro:Intro;
		private var blueCircleAnimation:Animation;
		private var greenCircleAnimation:Animation;
		private var render:Render;
		private var searchApartment:SearchApartment;
		private var gallery:Gallery;
		private var basicScreen:BasicScreen;
		private var chooser:Chooser;
		private var floorLayout:FloorLayout;
		private var garage:Garage;
		private var apartmentLayout:ApartmentLayout;
		private var about:AboutProjectApartments;
		private var aboutBusiness:AboutBusiness;
		private var aboutProjectBusiness:AboutProjectBusiness;
		private var aboutApartments:AboutApartments;
		private var contact:Contact;
		private var contactBusiness:ContactBusiness;
		private var faq:FAQ;
		private var discount:Discount;
		private var conditions:Conditions;
		private var floorLayoutBusiness:FloorLayoutBusiness;
		private var imageView:ImageView;
		private var floorLayoutBusinessSelling:FloorLayoutBusinessSelling;
		
		private var switchWindowDuration:Number = 0.5;
		private var currentScreen:DisplayObject = null;
		private var lastScreen:DisplayObject = null;
		
		public function Main() {
			//init();
			super();
		}
		
		override protected function construct():void
		{
			super.construct();
			
			Utilities.loadURL("xml/config.xml", loadConfig_Complete);
			
			var cMenu:ContextMenu = new ContextMenu();
			cMenu.hideBuiltInItems();
			this.contextMenu = cMenu;
			
		}
		
		override protected function addListeners():void
		{
			super.addListeners();
			model.addEventListener(GUIEvent.SWITCH_SCREEN, update);
		}
		
		override protected function removeListeners():void
		{
			super.removeListeners();
			//model.removeEventListener(GUIEvent.SWITCH_SCREEN, update);
		}
		
		override protected function update(event:Event):void
		{
			switch(event.type)
			{
				case GUIEvent.SWITCH_SCREEN:
					if (model.currentScreen.type == ScreenName.INTRO)
					{
						removeAll();
						initIntro();
					}
					else
					{
						switchScreen(model.currentScreen);
					}
					break;
				default:
					super.update(event);
					break;
			}
		}
		
		
		private function loadConfig_Complete(event:Event):void
		{
			var configXML:XML = XML(event.target.data);
			
			var screenXMLs:Array = new Array();
			for each (var item:XML in configXML.descendants("asset"))
			screenXMLs.push(String(item.@url));
			
			Utilities.loadMultipleURL(screenXMLs, loadXMLs_Complete);
		}
		
		private function loadXMLs_Complete(xmls:Array):void
		{
			model.screenXMLs = xmls;
			
			controller.showScreen(ScreenName.INTRO);
		}
		
		//{ region Intro
		
		private function initIntro():void {
			intro = new Intro();
			intro.addEventListener(ChooserEvent.CHOOSER_CLICKED, chooserClicked);
			addChild(intro);
		}
		
		private function chooserClicked(event:ChooserEvent):void {
			//removeObject(intro);
			initBasicScreen();
			basicScreen.setMenuMode(event.choosenOption);
			controller.showScreen(ScreenName.RENDER);
			/*switch(event.choosenOption) {
				case "apartments":
					//hideChooser();
					initBasicScreen();
					basicScreen.setMenuMode("apartments");
					initRender();
					break;
				case "business":
					//hideChooser();
					initBasicScreen();
					basicScreen.setMenuMode("business");
					initRender();
					break;
				default:
					throw new Error("Unknown option choosen");
			}*/
		}
		
		//} endregion
		
		//{ region Basic screen
		
		private function initBasicScreen():void {
			//TweenLite.to(basicScreen, 1, { alpha:1 } );
			basicScreen = new BasicScreen();
			//basicScreen.addEventListener(MenuEvent.CLICK, basicScreenMenuClick);
			addChild(basicScreen);
		}
		
		private function hideBasicScreen():void {
			removeObject(basicScreen);
		}
		
		private function basicScreenMenuClick(event:MenuEvent):void {
			var name:String = event.item;
			switch(name) {
				case "about":
					initAbout();
					break;
				case "aboutProjectBusiness":
					initAboutProjectBusiness();
					break;
				case "faq":
					initFAQ();
					break;
				case "apartments":
				case "business":
					initRender();
					break;
				case "gallery":
					initGallery();
					break;
				case "searchAll":
					initSearchApartment();
					break;
				case "description":
					initAboutApartments();
					break;
				case "garage":
					initGarage();
					break;
				case "contact":
					initContact();
					break;
				case "contactBusiness":
					initContactBusiness();
					break;
				case "logo":
					removeAll();
					initIntro();
					break;
				case "discount":
					initDiscount();
					break;
				case "conditions":
					initConditions();
					break;
				case "rent":
					initFloorLayoutBusiness();
					break;
				case "shop":
					initFloorLayoutBusinessSelling(name);
					break;
				case "catering":
					initFloorLayoutBusinessSelling(name);
					break;
				case "kindergarten":
					initFloorLayoutBusinessSelling(name);
					break;
				case "businessDescription":
					initAboutBusiness();
					break;
					
				default:
					throw new Error("Unknown menu name: " + name);
			}
		}
		
		//} endregion
		
		//{ region Render
		
		private function initRender():void {
			render = new Render("screen group " + basicScreen.mode.toUpperCase());
			//render.init(basicScreen.mode);
			render.addEventListener(RenderEvent.FLOOR_CLICKED, renderFloorClicked);
			switchScreen(render);
		}
		
		private function hideRender():void {
			removeScreen(render);
		}
		
		private function renderFloorClicked(event:RenderEvent):void {
			//hideRender();
			if (basicScreen.mode == "apartments") {
				initFloorLayout(event.floorNumber);
			} else {
				initFloorLayoutBusiness(event.floorNumber);
			}
		}
		
		//} endregion
		
		//{ region Search apartment
		
		private function initSearchApartment():void {
			searchApartment = new SearchApartment();
			searchApartment.addEventListener(ApartmentEvent.SELECTED, searchApartmentSelected);
			switchScreen(searchApartment);
		}
		
		private function searchApartmentSelected(event:ApartmentEvent):void {
			initApartmentLayout(event.item.roomID, event.item.floorID, "search");
		}
		
		private function hideSearchApartment():void {
			removeScreen(searchApartment);
		}
		
		//} endregion
		
		//{ region Gallery
		
		private function initGallery():void {
			gallery = new Gallery();
			switchScreen(gallery);
		}
		
		private function hideGallery():void {
			removeScreen(gallery);
		}
		
		//} endregion
		
		//{ region Floor layout
		
		private function initFloorLayout(floorNumber:int):void {
			//basicScreen.togglePreviousEnabled(true);
			floorLayout = new FloorLayout();
			floorLayout.showFloor(floorNumber);
			floorLayout.addEventListener(ApartmentEvent.SELECTED, floorLayoutRoomClicked);
			floorLayout.addEventListener(MenuEvent.PREVIOUS, floorLayoutMenuPrevious);
			switchScreen(floorLayout);
		}
		
		private function floorLayoutMenuPrevious(event:MenuEvent):void {
			initRender();
		}
		
		private function hideFloorLayout():void {
			removeScreen(floorLayout);
		}
		
		private function floorLayoutRoomClicked(event:ApartmentEvent):void {
			//hideFloorLayout();
			initApartmentLayout(event.item.roomID, event.item.floorID, "floorLayout");
		}
		
		//} endregion
		
		//{ region Floor layout business
		
		private function initFloorLayoutBusiness(floorNumber:int = 0):void {
			floorLayoutBusiness = new FloorLayoutBusiness();
			floorLayoutBusiness.addEventListener(MenuEvent.PREVIOUS, floorLayoutBusinessMenuPrevious);
			switchScreen(floorLayoutBusiness);
			floorLayoutBusiness.showFloor(floorNumber);
		}
		
		private function hideFloorLayoutBusiness():void {
			removeScreen(floorLayoutBusiness);
		}
		
		private function floorLayoutBusinessMenuPrevious(event:MenuEvent):void {
			initRender();
		}
		
		//} endregion
		
		//{ region Floor layout business selling
		
		private function initFloorLayoutBusinessSelling(sellingMode:String):void {
			floorLayoutBusinessSelling = new FloorLayoutBusinessSelling(sellingMode);
			//floorLayoutBusinessSelling.sellingMode = sellingMode;
			floorLayoutBusinessSelling.addEventListener(MenuEvent.PREVIOUS, floorLayoutBusinessSellingMenuPrevious);
			floorLayoutBusinessSelling.addEventListener(ApartmentEvent.CLICK, floorLayoutBusinessSellingRoomClicked);
			switchScreen(floorLayoutBusinessSelling);
		}
		
		private function floorLayoutBusinessSellingRoomClicked(event:ApartmentEvent):void {
			initApartmentLayout(event.item.roomID, 0, "floorLayoutBusinessSelling", true);
		}
		
		private function hideFloorLayoutBusinessSelling():void {
			removeScreen(floorLayoutBusinessSelling);
		}
		
		private function floorLayoutBusinessSellingMenuPrevious(event:MenuEvent):void {
			initRender();
		}
		
		//} endregion
		
		//{ region Garage
		
		private function initGarage():void {
			garage = new Garage();
			garage.addEventListener(MenuEvent.PREVIOUS, garageMenuPrevious);
			switchScreen(garage);
		}
		
		private function garageMenuPrevious(event:MenuEvent):void {
			initRender();
		}
		
		private function hideGarage():void {
			removeScreen(garage);
		}
		
		//} endregion
		
		//{ region Apartment layout
		
		private function initApartmentLayout(roomID:int, floorID:int, source:String, isBusiness:Boolean = false):void {
			//basicScreen.togglePreviousEnabled(true);
			apartmentLayout = new ApartmentLayout(roomID, floorID, source, isBusiness);
			//apartmentLayout.init(roomID, floorID, source, isBusiness);
			apartmentLayout.addEventListener(MenuEvent.PREVIOUS, apartmentLayoutPrevious);
			apartmentLayout.addEventListener(ApartmentEvent.CLICK, showImage);
			switchScreen(apartmentLayout);
		}
		
		private function showImage(event:ApartmentEvent):void {
			var image:DisplayObject = DisplayObject(event.item);
			initImageView(image);
		}
		
		private function apartmentLayoutPrevious(event:MenuEvent):void {
			if (apartmentLayout.source == "floorLayout") {
				initFloorLayout(int(event.item));
			} else if (apartmentLayout.source == "floorLayoutBusinessSelling") {
				initFloorLayoutBusinessSelling("shop");
			} else if (apartmentLayout.source == "search") {
				initSearchApartment();
			} else {
				throw new Error("Illegal source for apartments");
			}
		}
		
		private function hideApartmentLayout():void {
			removeScreen(apartmentLayout);
		}
		
		//} endregion
		
		//{ region About
		
		private function initAbout():void {
			about = new AboutProjectApartments();
			switchScreen(about);
		}
		
		private function hideAbout():void {
			removeScreen(about);
		}
		
		//} endregion
		
		//{ region About project business
		
		private function initAboutProjectBusiness():void {
			aboutProjectBusiness = new AboutProjectBusiness();
			switchScreen(aboutProjectBusiness);
		}
		
		private function hideAboutProjectBusiness():void {
			removeScreen(aboutProjectBusiness);
		}
		
		//} endregion
		
		//{ region About business
		
		private function initAboutBusiness():void {
			aboutBusiness = new AboutBusiness();
			switchScreen(aboutBusiness);
		}
		
		private function hideAboutBusiness():void {
			removeScreen(aboutBusiness);
		}
		
		//} endregion
		
		//{ region FAQ
		
		private function initFAQ():void {
			faq = new FAQ();
			switchScreen(faq);
		}
		
		private function hideFAQ():void {
			removeScreen(faq);
		}
		
		//} endregion
		
		//{ region Discount
		
		private function initDiscount():void {
			discount = new Discount();
			switchScreen(discount);
		}
		
		private function hideDiscount():void {
			removeScreen(discount);
		}
		
		//} endregion
		
		//{ region Conditions
		
		private function initConditions():void {
			conditions = new Conditions();
			switchScreen(conditions);
		}
		
		private function hideConditions():void {
			removeScreen(conditions);
		}
		
		//} endregion
		
		//{ region About apartments
		
		private function initAboutApartments():void {
			aboutApartments = new AboutApartments();
			switchScreen(aboutApartments);
		}
		
		private function hideAboutApartments():void {
			removeScreen(aboutApartments);
		}
		
		//} endregion
		
		//{ region Contact
		
		private function initContact():void {
			contact = new Contact();
			switchScreen(contact);
		}
		
		private function hideContact():void {
			removeScreen(contact);
		}
		
		//} endregion
		
		//{ region Contact business
		
		private function initContactBusiness():void {
			contactBusiness = new ContactBusiness();
			switchScreen(contactBusiness);
		}
		
		private function hideContactBusiness():void {
			removeScreen(contactBusiness);
		}
		
		//} endregion
		
		//{ region Image view
		
		private function initImageView(image:DisplayObject):void {
			imageView = new ImageView();
			//imageView.showImage(image);
			imageView.addEventListener(Event.COMPLETE, hideImageView);
			this.addChild(imageView);
		}
		
		private function hideImageView(event:Event):void {
			this.removeChild(imageView);
		}
		
		//} endregion
		
		//{ region Screen management
		
		private function switchScreen(screen:DisplayObject):void {
			if (currentScreen != null) {
				lastScreen = currentScreen;
				TweenLite.to(lastScreen, switchWindowDuration, { alpha:0, onComplete:screenHidden, onCompleteParams:[lastScreen] } );
			}
			
			screen.alpha = 0;
			addScreen(screen);
			//TweenLite.to(screen, switchWindowDuration, { alpha:1 } );
			TweenLite.to(screen, switchWindowDuration, { alpha:1, onComplete:screenShown, onCompleteParams:[screen] } );
			//currentScreen = screen;
		}
		
		private function screenHidden(screen:DisplayObject):void {
			removeScreen(screen);
		}
		
		private function screenShown(screen:DisplayObject):void {
			currentScreen = screen;
		}
		
		private function addScreen(screen:DisplayObject):void {
			if (!basicScreen)
				initBasicScreen();
			
			screen.x = basicScreen.mainMenu.x;
			screen.y = basicScreen.mainMenu.y + 45;
			basicScreen.addChildAt(screen, basicScreen.numChildren - 2);
		}
		
		private function removeScreen(screen:DisplayObject):void {
			if (basicScreen != null) {
				basicScreen.removeChild(screen);
				//currentScreen = null;
				screen = null;
			}
		}
		
		private function removeObject(object:DisplayObject):void {
			removeChild(object);
			object = null;
		}
		
		private function removeAll():void {
			if (intro != null)
				if (getChildIndex(intro) != -1) {
					removeChild(intro);
					intro = null;
				}
			if (currentScreen != null)
				if (basicScreen.getChildIndex(currentScreen) != -1) {
					basicScreen.removeChild(currentScreen);
					currentScreen = null;
				}
			if (basicScreen != null)
				if (getChildIndex(basicScreen) != -1) {
					removeChild(basicScreen);
					basicScreen = null;
				}
			
		}
		
		//} endregion
		
	}

}